<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/15
 **/

namespace app\admin\service;

use app\admin\item\AdminLoader;
use app\admin\pipeline\ItemAdapter;
use app\admin\validate\AdminValidate;
use app\common\model\AdminModel;
use think\facade\Session;


class AdminService
{

    public static function getList($params)
    {
        $pagination = filter_pagination(@$params['page'], @$params['limit']);
        $loader = new AdminLoader();
        $where = $loader->addWhere(['status', '<>', -1])->addWhere(['id', '<>', 1])->loadWhere();
        $data = [];
        $query = AdminModel::where($where)->order(["id" => "asc"]);
        $data['total'] = $query->count();
        $data['dataList'] = [];
        if ($data['total']) {
            $data['dataList'] = $query->limit($pagination['start'], $pagination['limit'])->select()->toArray();
        }
        return $data;
    }


    public static function save($params)
    {
        $loader = new AdminLoader();
        $data = $loader->addVal($params)
            ->addFilterIn("role_ids")
            ->addVerify(AdminValidate::class, AdminValidate::SCENE_SAVE)->loadItem();
        $adapter = new ItemAdapter(AdminModel::class);
        $adapter->hookAdd("admin_role_hook", ["admin_id" => "?id", "role_ids" => $params['role_ids']]);
        $res = $adapter->save($data);
        if ($res) {
            return $adapter->getId();
        } else {
            return false;
        }
    }

    public static function getByID($id)
    {
        $adapter = new  ItemAdapter(AdminModel::class);
        return $adapter->id($id)->getModel();
    }


    public static function del($params)
    {
        $adapter = new ItemAdapter(AdminModel::class);
        return $adapter->whereUpdate([["id", "in", $params['id']], ["id", "<>", 1]], ["status" => -1]);
    }


    public static function getAdminId()
    {
        $admin_id = (int)Session::get("admin_id");
        return $admin_id;
    }


    public static function userSetting($params, $admin_id = 0)
    {
        if (empty($admin_id)) {
            $admin_id = self::getAdminId();
        }
        $loader = new AdminLoader();
        $data = $loader->addVal($params)
            ->addFilterIn("id,username")
            ->addVerify(AdminValidate::class, AdminValidate::SCENE_USER_SETTING)
            ->loadItem();
        $adapter = new ItemAdapter(AdminModel::class);
        $res = $adapter->get($admin_id)->save($data);
        return $res;
    }

    public static function userPassword($password, $admin_id = 0)
    {

        if (empty($admin_id)) {
            $admin_id = Session::get("admin_id");
        }
        $loader = new AdminLoader();
        $data = $loader->addVal(["password" => $password])
            ->addVerify(AdminValidate::class, AdminValidate::SCENE_PASSWORD)
            ->loadItem();
        $adapter = new ItemAdapter(AdminModel::class);
        $res = $adapter->get($admin_id)->save($data);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }
}